package com.zijidelu.luframework.utils.spring.beans.generic;

import org.springframework.core.ResolvableType;

public class SpringBeanTypeUtil {
    /**
     * 解析spring bean的泛型
     *
     * @param bean        spring bean
     * @param targetClass 目标类，bean解析为目标类
     * @return spring定义的泛型数组
     */
    public static ResolvableType[] resolvableTargetClassTypes(Object bean, Class<?> targetClass) {
        // 1. 从Bean实例创建 ResolvableType
        ResolvableType resolvableType = ResolvableType.forInstance(bean);

        // 2. 解析为 IService<> 类型
        ResolvableType serviceType = resolvableType.as(targetClass);

        // 3. 获取第一个泛型参数
        return serviceType.getGenerics();
    }

    /**
     * 解析spring bean的泛型
     *
     * @param bean spring bean
     * @return spring定义的泛型类
     */
    public static ResolvableType resolvableType(Object bean) {
        return ResolvableType.forInstance(bean);
    }

    /**
     * 获取spring定义的泛型类的真实类型
     *
     * @param resolvableType spring定义的泛型类
     * @return 泛型类的真实类型
     */
    public static Class<?> getRawType(ResolvableType resolvableType) {
        return resolvableType.resolve();
    }

}
